package com.huike.clues.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huike.clues.domain.TbCourse;
import com.huike.clues.domain.vo.PageResult;
import com.huike.clues.dto.CoursePageDTO;
import com.huike.clues.mapper.BusinessMapper2;
import com.huike.clues.mapper.CourseMapper;
import com.huike.clues.service.CourseService;
import com.huike.common.core.domain.MPentity.Business;
import com.huike.common.core.domain.MPentity.Course;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.exception.BaseException;
import com.huike.common.utils.bean.BeanUtils;
import com.huike.common.utils.uuid.UUID;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class courseServiceImpl implements CourseService {

    @Resource
    CourseMapper courseMapper;

    @Resource
    BusinessMapper2 businessMapper2;

    /**
     * 新增课程
     *
     * @param tbCourse
     */
    @Override
    public void saveCourse(TbCourse tbCourse) {
        // 创建课程实体类
        Course course = new Course();
        // 将tbCourse赋值给course
        BeanUtils.copyProperties(tbCourse, course);
        // 随机生成8位数课程编号
        String code = UUID.randomUUID().toString().replace("-", "").substring(0, 8);
        course.setCode(code);
        // 添加创建时间
        course.setCreateTime(LocalDateTime.now());
        // 新增课程
        courseMapper.insert(course);
    }

    /**
     * 分页查询课程
     *
     * @param coursePageDTO
     * @return
     */
    @Override
    public TableDataInfo list(CoursePageDTO coursePageDTO) {
        Course course = new Course();
        BeanUtils.copyProperties(coursePageDTO, course);
        LocalDate begin = null;
        LocalDate end = null;


        String beginCreateTime = coursePageDTO.getParams().get("beginCreateTime").toString();
        String endCreateTime = coursePageDTO.getParams().get("endCreateTime").toString();

        if (!StringUtils.isEmpty(beginCreateTime) && !StringUtils.isEmpty(endCreateTime)) {
            // 开始时间和结束时间
            begin = LocalDate.parse(beginCreateTime);
            end = LocalDate.parse(endCreateTime);
        }

        //1. 设置当前页与页面大小
        Page<Course> page = new Page<>(coursePageDTO.getPageNum(), coursePageDTO.getPageSize());
        //2. 创建分页需要条件
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(!StringUtils.isEmpty(course.getName()), Course::getName, course.getName());
        wrapper.eq(!StringUtils.isEmpty(course.getCode()), Course::getCode, course.getCode());
        wrapper.eq(!StringUtils.isEmpty(course.getApplicablePerson()), Course::getApplicablePerson, course.getApplicablePerson());
        wrapper.eq(!StringUtils.isEmpty(course.getSubject()), Course::getSubject, course.getSubject());
        //判断时间条件是否为空,为空就不查询
        if (!StringUtils.isEmpty(begin) && !StringUtils.isEmpty(end)) {
            wrapper.between(Course::getCreateTime, begin, end);
        }

        //3. 调用selectPage方法实现分页，分页的结果会被封装到Page对象中
        courseMapper.selectPage(page, wrapper);

        List<Course> courseList = page.getRecords();

        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getRecords());

        return tableDataInfo;
    }

    /**
     * 修改课程信息
     *
     * @param tbCourse
     */
    @Override
    public void update(TbCourse tbCourse) {
        Course course = new Course();
        BeanUtils.copyProperties(tbCourse, course);

        LambdaQueryWrapper<Business> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Business::getCourseId, course.getId());

        List<Business> businessList = businessMapper2.selectList(wrapper);

        if (!CollectionUtils.isEmpty(businessList)) {
            throw new BaseException("无法修改,本课程已被应用");
        }

        courseMapper.updateById(course);
    }

    /**
     * 课程下拉列表
     *
     * @param subject
     * @return
     */
    @Override
    public List<Course> listSelect(String subject) {
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper();
        wrapper.eq(!StringUtils.isEmpty(subject), Course::getSubject, subject);

        List<Course> courseList = courseMapper.selectList(wrapper);

        return courseList;
    }

    /**
     * 获取课程管理详细信息
     *
     * @param id
     * @return
     */
    @Override
    public Course getById(Integer id) {
        Course course = courseMapper.selectById(id);
        return course;
    }

    /**
     * 根据Id批量删除课程
     *
     * @param ids
     */
    @Override
    public void deleted(int[] ids) {

        // 创建list集合 用于存储每个id值
        List<Integer> idList = new ArrayList<>(ids.length);
        // 遍历数组拿到每个id值
        for (int id : ids) {
            idList.add(id);
        }
        LambdaQueryWrapper<Business> wrapper = new LambdaQueryWrapper<>();
        // 根据课程id查询合同表
        wrapper.in(Business::getCourseId, idList);
        List<Business> businessList = businessMapper2.selectList(wrapper);

        // 判读合同中是否包含课程数据 如果有则不删除
        if (!CollectionUtils.isEmpty(businessList)) {
            throw new BaseException("合同中包含该课程,无法删除");
        }

        // 删除课程
        courseMapper.deleteBatchIds(idList);
    }
}
